<template>
  <el-dialog v-model="visible" :before-close="closeDialog" class="bulletin-dialog custom-dialog"
    modal-class="dialog-center" :show-close='false' :close-on-press-escape="false" append-to-body
    :close-on-click-modal="false" width="628px" z-index="2099">
    <div class="bulletin-body" :class="[bulletinType != 'IMG' && 'bulletin-body-bg']" :style="{ height: `${imgWidth}px` }"
      v-loading="loading">
      <div class="bulletin-header" :class="[bulletinType == 'IMG' && 'bulletion-header-pos']">
        <div class="bulletin-title">
          {{ bulletin.title }}
        </div>
        <svg class="close-icon" @click="closeDialog">
          <use xlink:href="#keowledge-danchuangguanbi1"></use>
        </svg>
      </div>
      <div class="bulletion-icon-box" v-if="bulletinType == 'TEXT'">
        <img src="../../assets/images/common/bulletin_icon.png">
      </div>
      <div class="bulletin-contianer" v-if="bulletinType == 'TEXT'">
        <div class="bulletin-contianer-content" :style="{ maxHeight: contianerHeight }">
          <el-scrollbar>
            <div class="bulletin-contianer-content-text" v-html="bulletin.content"></div>
          </el-scrollbar>
        </div>
      </div>
      <img class="bulletin-img" id="bulletin-img" :src="bulletin.content" v-if="bulletinType == 'IMG'"
        @click="closeDialog" @load="imgLoad">
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import {
  ref,
  reactive,
  toRefs,
  watch,
  onMounted,
  getCurrentInstance,
  computed
} from 'vue'

import { useRouter, useRoute } from "vue-router"
import { getBulletinList } from '@/api/common'
const router = useRouter();

const emit = defineEmits([""])
const props = defineProps({
  remove: Function,
})

const visible = ref(false)
const bulletin = ref({})
const bulletinType = ref('')
const imgWidth = ref()
const loading = ref(false)
onMounted(() => {
  init()
})
const contianerHeight = computed(() => {
  if (document.body.offsetHeight < 600) {
    return 'calc(100vh - 200px)'
  } else {
    return '470px'
  }
})
function init() {
  visible.value = true
  loading.value = true
  getBulletinList().then(res => {
    if (res.data.code == 0) {
      bulletin.value = res.data.data || {}
      if (bulletin.value.content && bulletin.value.content.length > 0) {
        bulletin.value.content = bulletin.value.content.replace('<img ', `<img style="max-width: 548px;"`)
        bulletinType.value = bulletin.value.contentType
      } else {
        closeDialog()
      }
    }
  }).finally(resF => {
    loading.value = false
  }).catch(er => {
    loading.value = false
    closeDialog()
  })
}
function closeDialog() {
  visible.value = false
  props.remove && props.remove()
}
function imgLoad(e) {
  imgWidth.value = document.getElementById('bulletin-img')?.clientHeight
}
</script>
<style lang="scss">
.bulletin-dialog {
  .bulletin-body {
    min-height: 320px;
    position: relative;

    .bulletion-icon-box {
      width: 170px;
      height: 170px;
      position: absolute;
      right: 24px;
      top: 0px;
      z-index: 0;

      img {
        width: 100%;
      }
    }

    .bulletin-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 24px 24px;
      box-sizing: border-box;
      position: relative;
      z-index: 1;

      .bulletin-title {
        font-size: 20px;
        color: #363b4c;
        @include SourceHanSansCN-Bold;
      }

      .close-icon {
        width: 16px;
        height: 16px;
        cursor: pointer;
      }
    }

    .bulletion-header-pos {
      position: absolute;
      top: 0px;
      width: 100%;
      text-align: right;
      justify-content: flex-end;

      .bulletin-title {
        display: none;
      }
    }

    .bulletin-img {
      width: 100%;
      cursor: pointer;
    }

    .bulletin-contianer {
      padding: 0px 24px 24px;

      .bulletin-contianer-content {
        padding: 12px 0px;
        background: linear-gradient(153deg, rgba(255, 255, 255, 0.58) 0%, rgba(255, 255, 255, 0.16) 100%);
        border-radius: 6px;
        position: relative;
        overflow: hidden;

        &::before {
          content: '';
          position: absolute;
          width: 100%;
          height: 100%;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          z-index: 0;
          border-radius: 6px;
          /* 设置你需要的border-radius值 */
          border-image: linear-gradient(210deg, rgba(255, 255, 255, 0.3700000047683716), rgba(255, 255, 255, 0.550000011920929)) 1 1;
          border-image-width: 2px;
        }

        .bulletin-contianer-content-text {
          position: relative;
          z-index: 1;
          padding: 0px 16px;
        }
      }
    }
  }

  .bulletin-body-bg {
    background-image: url('../../assets/images/common/bulletin_bg.png');

    .bulletin-header {
      &::after {
        content: '';
        position: absolute;
        left: 24px;
        width: 58px;
        bottom: 16px;
        height: 3px;
        border-radius: 8px 0px 8px 0px;
        background: linear-gradient(to right, #1E6FFF, #36F8FC);
      }
    }
  }
}
</style>